package com.example.intelligentattendancesystem.Utils;

import com.example.intelligentattendancesystem.mapping.LeaveMapping;
import com.example.intelligentattendancesystem.mapping.WorkMapping;
import com.example.intelligentattendancesystem.pojo.Leave;
import com.example.intelligentattendancesystem.pojo.Work;
import com.example.intelligentattendancesystem.service.impl.WorkServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @className Utile
 * @since 1.0
 */
@Component
public class Utile {
    @Autowired
    LeaveMapping leaveMapping;
    @Autowired
    WorkMapping workMapping;


    /**
     * 获取不重复的,有效的记录
     * @param leaves
     * @return
     */
    public List<Leave> getNoReviewLeave(List<Leave> leaves){
        List<Leave> ret = new ArrayList<>();
        List<Leave> ret2 = new ArrayList<>();
        List<Leave> leaves1 = leaveMapping.selectLeaveByUserId(leaves.get(0).getUserId());
        int flge=1;
        for (Leave newLeave:leaves) {
            for (int i=0;i<leaves1.size();i++) {
                if(leaves1.get(i).getLeaveStartTime().equals(newLeave.getLeaveStartTime()))
                {
                    flge=0;
                }
            }
            if(flge==1){
                ret.add(newLeave);
            }
        }
        WorkServiceImpl workService = new WorkServiceImpl();
        //如果该请假记录不在work中，就无效
        List<Work> works = workMapping.selectWorkByUserId(leaves.get(0).getUserId());
        for (int i = 0; i < ret.size(); i++) {
            for (int j = 0; j < works.size(); j++) {
                if(workService.isBetween(works.get(j),ret.get(i))){
                    ret2.add(ret.get(i));
                    break;
                }
            }
        }
        return ret2;
    }

    /**
     * 初始化leaves
     * @param leaves
     * @return
     */
    public List<Leave> initLeaves(List<Leave> leaves){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(new Date());
        for (Leave leave:leaves) {
            leave.setDate(format);
        }
        return leaves;
    }

    /**
     * 获取时刻表
     * @return
     */
    public List<Work> getTimeTable(){
        List<Work> works = workMapping.selectAllWork();
        List<Work> w = new ArrayList<>();
        for (Work work:
             works) {
            if(work.getWeek().equals("-1")){
                w.add(work);
            }
        }
        return w;
    }

    /**
     * 获取当前时间到n天前的所有leave（请假申请）数据
     * @param n
     * @return
     */
    public List<Leave> getLeaveBetweenTime(int n) throws ParseException {
        List<Leave> leaves = leaveMapping.selectAllLeave();
        List<Leave> leaves1 = new ArrayList<>();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();

        //获取当前时间搓
        String nowDate = df.format(new Date());
        Date date1 = df.parse(nowDate);
        cal.setTime(date1);
        long nowTime = cal.getTimeInMillis();

        long leaveTime = 0;
        Date date2=null;
        for (Leave leave:leaves) {
            String date = leave.getDate();
            //每次请假的时间搓
            date2 = df.parse(date);
            cal.setTime(date2);
            leaveTime=cal.getTimeInMillis();
            if(nowTime-leaveTime<=n*24*60*60*1000){
                leaves1.add(leave);
            }
        }
        return leaves1;
    }

}
